package com.powernode.interceptor;

import com.powernode.common.Constants;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    /**
     *进入controller之前需要调用的方法
     * @param request  请求对象
     * @param response 响应对象
     * @param handler 要访问的Controller对象
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session中的数据
        Object user = request.getSession().getAttribute(Constants.SESSION_USER_KEY);
        if (user!=null){
            return true;
        }else{
            //执行重定向操作
            response.sendRedirect(request.getContextPath()+"/index.jsp");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
